package com.weijian.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weijian.common.ApiResponse;
import com.weijian.dal.model.NotificationMessage;
import com.weijian.service.NotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息通知控制器
 * 提供用户消息通知相关的RESTful API接口
 *
 * @author weijian
 */
@RestController
@RequestMapping("/api/notifications")
@Tag(name = "消息通知", description = "用户消息通知管理相关接口")
public class NotificationController {

    @Resource
    private NotificationService notificationService;

    /**
     * 获取用户消息列表
     * 分页获取指定用户的所有消息通知
     */
    @Operation(summary = "获取用户消息列表", description = "分页获取指定用户的所有消息通知")
    @GetMapping
    public ApiResponse<IPage<NotificationMessage>> getMessages(
            @Parameter(description = "用户ID", required = true, example = "1")
            @RequestParam Long userId,
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页条数", example = "20")
            @RequestParam(defaultValue = "20") int size) {

        IPage<NotificationMessage> messages = notificationService.getUserMessages(userId, page, size);
        return ApiResponse.success(messages);
    }

    /**
     * 获取未读消息数量
     * 获取指定用户的未读消息总数
     */
    @Operation(summary = "获取未读消息数量", description = "获取指定用户的未读消息总数")
    @GetMapping("/unread-count")
    public ApiResponse<Integer> getUnreadCount(
            @Parameter(description = "用户ID", required = true, example = "1")
            @RequestParam Long userId) {
        int count = notificationService.getUnreadCount(userId);
        return ApiResponse.success(count);
    }

    /**
     * 获取各类型未读消息数量
     * 按消息类型分组统计指定用户的未读消息数量
     */
    @Operation(summary = "获取各类型未读消息数量", description = "按消息类型分组统计指定用户的未读消息数量")
    @GetMapping("/unread-count-by-type")
    public ApiResponse<List<java.util.Map<String, Object>>> getUnreadCountByType(
            @Parameter(description = "用户ID", required = true, example = "1")
            @RequestParam Long userId) {
        List<java.util.Map<String, Object>> counts = notificationService.getUnreadCountByType(userId);
        return ApiResponse.success(counts);
    }

    /**
     * 标记消息已读
     * 将指定消息标记为已读状态
     */
    @Operation(summary = "标记消息已读", description = "将指定消息标记为已读状态")
    @PostMapping("/{messageId}/read")
    public ApiResponse<String> markAsRead(
            @Parameter(description = "消息ID", required = true, example = "1")
            @PathVariable Long messageId,
            @Parameter(description = "用户ID", required = true, example = "1")
            @RequestParam Long userId) {

        boolean success = notificationService.markAsRead(userId, messageId);
        if (success) {
            return ApiResponse.success("已标记为已读");
        } else {
            return ApiResponse.error(401, "消息不存在或无权操作");
        }
    }

    /**
     * 批量标记消息已读
     * 批量将多条消息标记为已读状态
     */
    @Operation(summary = "批量标记消息已读", description = "批量将多条消息标记为已读状态")
    @PostMapping("/batch-read")
    public ApiResponse<String> batchMarkAsRead(
            @Parameter(description = "用户ID", required = true, example = "1")
            @RequestParam Long userId,
            @Parameter(description = "消息ID列表", required = true)
            @RequestBody @NotNull List<Long> messageIds) {

        boolean success = notificationService.batchMarkAsRead(userId, messageIds);
        if (success) {
            return ApiResponse.success("批量标记成功");
        } else {
            return ApiResponse.error(400, "操作失败");
        }
    }

    /**
     * 删除消息
     * 删除指定用户的某条消息
     */
    @Operation(summary = "删除消息", description = "删除指定用户的某条消息")
    @DeleteMapping("/{messageId}")
    public ApiResponse<String> deleteMessage(
            @Parameter(description = "消息ID", required = true, example = "1")
            @PathVariable Long messageId,
            @Parameter(description = "用户ID", required = true, example = "1")
            @RequestParam Long userId) {

        boolean success = notificationService.deleteMessage(userId, messageId);
        if (success) {
            return ApiResponse.success("删除成功");
        } else {
            return ApiResponse.error(400, "消息不存在或无权操作");
        }
    }

    /**
     * 发送测试消息（开发测试用）
     * 用于开发测试阶段手动发送消息通知
     */
    @Operation(summary = "发送测试消息", description = "用于开发测试阶段手动发送消息通知")
    @PostMapping("/test/send")
    public ApiResponse<String> sendTestMessage(
            @Parameter(description = "消息实体", required = true)
            @RequestBody NotificationMessage message) {
        notificationService.sendNotification(message);
        return ApiResponse.success("消息发送成功");
    }
}